package com.example.zz.example.badgecount;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import com.example.zz.example.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Describe:
 *
 * @author zhangzhuo18.
 * Date: 2022/11/9
 */
public class BadgeCountActivity extends AppCompatActivity implements View.OnClickListener {


    private static int badgeCount = 100; //角标数量
    NotificationManager notificationManager;
    public static int notificationId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_badge_count);

        findViewById(R.id.badge_count_reduce_btn).setOnClickListener(this);
        findViewById(R.id.badge_count_add_btn).setOnClickListener(this);
        //设置角标数量
//        BadgeUtils.setBadgeCount(this,100);
//        BadgeUtils.setBadgeDrawableBounds();
        BadgeNumberManager.from(this).setBadgeNumber(39);
    }

    private static String getLauncherClassName(Context context) {
        ComponentName launchComponent = getLauncherComponentName(context);
        if (launchComponent == null) {
            return "";
        } else {
            return launchComponent.getClassName();
        }
    }

    private static ComponentName getLauncherComponentName(Context context) {
        Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context
                .getPackageName());
        if (launchIntent != null) {
            return launchIntent.getComponent();
        } else {
            return null;
        }
    }

    private static boolean setHuaweiBadge(int count, Context context) {
        try {
            String launchClassName = getLauncherClassName(context);
            if (TextUtils.isEmpty(launchClassName)) {
                return false;
            }
            Bundle bundle = new Bundle();
            bundle.putString("package", context.getPackageName());
            bundle.putString("class", launchClassName);
            bundle.putInt("badgenumber", count);
            context.getContentResolver().call(Uri.parse("content://com.huawei.android.launcher" +
                    ".settings/badge/"), "change_badge", null, bundle);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean setNotificationBadge(int count, Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService
                (Context.NOTIFICATION_SERVICE);
        if (notificationManager == null) {
            return false;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // 8.0之后添加角标需要NotificationChannel
            NotificationChannel channel = new NotificationChannel("badge", "badge",
                    NotificationManager.IMPORTANCE_DEFAULT);
            channel.setShowBadge(true);
            notificationManager.createNotificationChannel(channel);
        }
        Intent intent = new Intent(context, BadgeCountActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(context, "badge")
                .setContentTitle("应用角标")
                .setContentText("您有" + count + "条未读消息")
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap
                        .ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setChannelId("badge")
                .setNumber(count)
                .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL).build();
        // 小米
        try {
            Field field = notification.getClass().getDeclaredField("extraNotification");
            Object extraNotification = field.get(notification);
            Method method = extraNotification.getClass().getDeclaredMethod("setMessageCount", int
                    .class);
            method.invoke(extraNotification, count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        notificationManager.notify(notificationId++, notification);
        return true;
    }

    private static boolean setSamsungBadge(int count, Context context) {
        try {
            String launcherClassName = getLauncherClassName(context);
            if (TextUtils.isEmpty(launcherClassName)) {
                return false;
            }
            Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
            intent.putExtra("badge_count", count);
            intent.putExtra("badge_count_package_name", context.getPackageName());
            intent.putExtra("badge_count_class_name", launcherClassName);
            context.sendBroadcast(intent);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.badge_count_reduce_btn:
                //Android原生方式 角标数量减1
                // BadgeUtils.setBadgeCount(this,--badgeCount);
                if (Build.MANUFACTURER.equalsIgnoreCase("HUAWEI")) {
                    setHuaweiBadge(--badgeCount, this);
                } else if (Build.MANUFACTURER.equalsIgnoreCase("Samsung")) {
                    setSamsungBadge(--badgeCount, this);
                    setNotificationBadge(badgeCount, this);
                    finish();
                } else {
                    setSamsungBadge(--badgeCount, this);
                    setNotificationBadge(badgeCount, this);
                    finish();
                }
                break;
            case R.id.badge_count_add_btn:

                if (Build.MANUFACTURER.equalsIgnoreCase("HUAWEI")) {
                    setHuaweiBadge(++badgeCount, this);
                } else if (Build.MANUFACTURER.equalsIgnoreCase("Samsung")) {
                    setSamsungBadge(++badgeCount, this);
                    setNotificationBadge(badgeCount, this);
                    finish();
                } else {
                    setSamsungBadge(++badgeCount, this);
                    setNotificationBadge(badgeCount, this);
                    finish();
                }

                break;

        }
    }
}
